$(document).ready(function (){
require(['inputdrop',
		 'tab',
		 'workorder'] ,function (inputdrop ,tab,workorder){

var deviceType;
var eventName;
um_ajax_get({
	url:"siem/logalarm/queryBacDeviceType",
	paramObj : {},
	isLoad:true,
	maskObj:"body",
	successCallBack:function(data){
		deviceType = data;
		um_ajax_get({
			url:"siem/logalarm/queryEventNameList",
			isLoad:true,
			maskObj:"body",
			successCallBack:function(data1){
				eventName = data1;
				alarm_event_policy_list();
			}
		});
	}
});

var index_list_url = "siem/logalarm/queryDiyTactic";

var index_list_col_header = [
							  {name:"",text:"" ,hideSearch:"hide",width:3},
							  {text:'策略名称',name:"tacticName",align:"left",width:17},
							  {text:'策略状态',name:"tacticStatusName" ,align:"left",width:13,searchRender:function (el){
									var data = [
													{text:"----" ,id:"-1"},
							  						{text:"启用" ,id:"1"},
							  						{text:"停用" ,id:"0"}
										  		];
									g_formel.select_render(el ,{
										data : data,
										name : "tacticStatus"
									});
								}},
							  {text:'日志发生源设备类型',name:"deviceTypeName",align:"left",width:18,hideSearch:true},
							  /*{text:'样本名称',name:"sampleName"},
							  {text:'关键字(字典表)',name:" keywordDictName"},
							  {text:'关键字(自定义)',name:"keywordName"},*/
							  {text:'生成的事件名称',name:"eventTypeName",align:"left",width:17,searchRender:function (el){
									var data = [
													{text:"----" ,id:"-1"}
										  		];
									for (var i = 0; i < eventName.length; i++) {
										data.push({text:eventName[i].eventName,id:eventName[i].eventId});
									}
									g_formel.select_render(el ,{
										data : data,
										name : "eventType"
									});
								}},
								{text:'修改人',name:'updateUser',align:"left",width:16},
								{text:'修改时间',name:'updateTime',align:"left",width:16,searchRender:function(el){
									index_render_div(el ,{
										type : "date",
										startKey : "startDate",
										endKey : "endDate"
									});
							  }}
							];
var index_list_col_oper = [
				  			  {icon:"rh-icon rh-edit" ,text:"修改" ,aclick:edit_template_init},
				  			  {icon:"rh-icon rh-delete" ,text:"删除" ,aclick:index_list_delete}
			   			  ];
var add_url = "siem/logalarm/addDiyTactic";
var update_url = "siem/logalarm/updDiyTactic";
var delete_url = "siem/logalarm/delDiyTactic";

event_init();

function event_init()
{
	// 新增按钮点击事件
	$("#add_btn").click(function (){
		edit_template_init();
	});

	index_list_search_render($("um-search") ,{
		cbf : function (val){
			g_grid.refresh($("#table_div") ,{
				paramObj : {tacticName : val}
			})
		},
		placeholder : "请输入策略名称"
	})
}

function alarm_event_policy_list()
{
	g_grid.render($("#table_div"),{
		 header:index_list_col_header,
		 oper: index_list_col_oper,
		 operWidth:"100px",
		 dbIndex:1,
		 url:index_list_url,
		 queryBefore:function (paramObj){
			paramObj.sampleId = paramObj.sampleName;
			paramObj.keywordDiy = paramObj.keywordName;
			paramObj.keywordDict = paramObj.keywordDictName
		},
		 isLoad : true,
		 maskObj : "body",
		 dbClick:list_detail,
		 hasBorder : false,
		 autoHeight : true,
		 showCount : true
	});
}

function list_detail(rowData)
{
	$.ajax({
		type: "GET",
		url: "module/policy_manage/alarm_event_policy_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=detail_template]"),{
				width:"500px",
				init:init,
				title:"日志报警事件策略详细信息",
				isDetail:true,
				autoHeight:true
			});
		}
	});

	function init(el)
	{
		el.umDataBind("render",rowData);
	}
}
function index_list_delete(rowData)
{
	g_dialog.operateConfirm("确认删除此记录么" ,{
		saveclick : function (){
			um_ajax_post({
				url : delete_url,
				paramObj : rowData,
				successCallBack : function(data){
					alarm_event_policy_list();
					g_dialog.operateAlert();
				},
				failCallBack:function(){
					g_dialog.operateAlert();
				}
			});
		}
	});
}

function edit_template_init(rowData)
{
	var title = rowData ? "日志报警事件策略修改" : "日志报警事件策略添加";
	$.ajax({
		type: "GET",
		url: "module/policy_manage/alarm_event_policy_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=edit_template]"),{
				width:"500px",
				init:init,
				saveclick:save_click,
				title:title,
				autoHeight:"autoHeight"
			});
		}
	});

	function init(el)
	{
		g_validate.init(el);
		el.find("[name=keywordForm]").click(function(){
			var tmp = $(this).val();
			if(tmp == "1")
			{
				el.find("[data-id=keywordDict]").removeAttr("disabled");
				el.find("[data-id=keywordDiy]").attr("disabled","disabled");
			}
			else
			{
				el.find("[data-id=keywordDict]").attr("disabled","disabled");
				el.find("[data-id=keywordDiy]").removeAttr("disabled");
			}
		});
		var deviceBuffer = [{typeName:"----" ,typeId:"-1"}];
		for (var i = 0; i < deviceType.length; i++) {
			deviceBuffer.push(deviceType[i]);
		}
		g_formel.selectEl_render(el.find("[data-id=deviceType]"),{
			data:deviceBuffer,
			text:"typeName",
			id:"typeId"
		});
		
		el.find("[data-id=deviceType]").change(function(){
			sample_keyword_get(el,{typeId:$(this).val()});
		})
		var eventBuffer = [{eventName:"----" ,eventId:"-1"}];
		for (var i = 0; i < eventName.length; i++) {
			eventBuffer.push(eventName[i]);
		}
		g_formel.selectEl_render(el.find("[data-id=eventType]"),{
			data:eventBuffer,
			text:"eventName",
			id:"eventId"
		});


		if(rowData)
		{
			el.umDataBind("render",rowData);
			el.find("[name=keywordForm][value="+rowData.keywordForm+"]").click();
			el.find("select").trigger("change");
		}
		else{
			sample_keyword_get(el);
		}
		
	}

	function save_click(el,saveObj)
	{
		if(!g_validate.validate(el))
		{
			return false;
		}
		var url = add_url;
		if(rowData)
		{
			url = update_url;
			saveObj.preTacticStatus = rowData.tacticStatus;
			saveObj.tacticId = rowData.tacticId;
		}
		if (saveObj.keywordForm == 0) {
			saveObj.keywordName = saveObj.keywordDiy;
			if (saveObj.keywordDiy == "" && saveObj.sampleId == "") 
			{
				g_dialog.operateAlert(el, "样本名称,关键字(自定义)请至少填写一项。","error");
				return false;
			}
		}
		else {
			saveObj.keywordName = saveObj.keywordDict;
			if (saveObj.keywordDict == "" && saveObj.sampleId == "") 
			{
				g_dialog.operateAlert(el, "样本名称,关键字(字典表)请至少填写一项。","error");
				return false;
			}
		}
		um_ajax_post({
			url:url,
			paramObj:saveObj,
			isLoad :true,
			maskObj:"body",
			successCallBack:function(data){
				g_dialog.hide(el);
				g_dialog.operateAlert();
				alarm_event_policy_list();
			}
		});
	}	
}

function sample_keyword_get(el,opt){
	typeId = -1
	if(opt && opt.typeId)
	{
		typeId = opt.typeId
		el.find("[data-id=sampleId]").find("option").remove()
		el.find("[data-id=keywordDict]").find("option").remove()
	}
	um_ajax_get({
		url:"siem/logalarm/getSampleCodeList",
		paramObj:{devtypeId:typeId},
		isLoad :true,
		maskObj:"body",
		successCallBack:function(data){
			var sampleBuffer = [{pattenName:"----" ,sampleId:"-1"}];
			for (var i = 0; i < data.length; i++) {
				sampleBuffer.push(data[i]);
			}
			g_formel.selectEl_render(el.find("[data-id=sampleId]"),{
				data:sampleBuffer,
				text:"pattenName",
				id:"sampleId"
			});
			um_ajax_get({
				url:"siem/logalarm/getKeywordCodeList",
				paramObj:{typeId:typeId},
				isLoad :true,
				maskObj:"body",
				successCallBack:function(data){
					var keyWordBuffer = [{keywordsCode:"----" ,keywordsId:"-1"}];
					for (var i = 0; i < data.length; i++) {
						keyWordBuffer.push(data[i]);
					}
					g_formel.selectEl_render(el.find("[data-id=keywordDict]"),{
						data:keyWordBuffer,
						text:"keywordsCode",
						id:"keywordsId"
					});
				}
			})
		}
	})
}
});
});